#include<bits/stdc++.h>

#define int long long
#define INF 4485090715960753727
#define maxn 200005

using namespace std;

inline int read(){
    int lre=0,f=1;
    char c=getchar();
    while(c<'0'||c>'9'){
        if(c=='-'){
            f=-1;
        }
        c=getchar();
    }
    while(c>='0'&&c<='9'){
        lre=(lre<<3)+(lre<<1)+(c-'0');
        c=getchar();
    }
    return lre*f;
}

int n,m;
int arr[maxn],brr[maxn],ind[maxn];
//int dp1[maxn],dp2[maxn];
//int nxt1[maxn],nxt2[maxn];

/*int for_max(int a[]){
    int lre=-INF;
    for(int i=1;i<=m;i++){
        lre=max(lre,a[i]);
    }
    return lre;
}*/

void show_arr(int a[]){
    cout<<"[";
    for(int i=1;i<=m;i++){
        if(a[i]<0){
            cout<<"X";
        }else{
            cout<<a[i];
        }
        if(i!=m)cout<<",";
    }
    cout<<"]"<<endl;
}

//range add,point change,range query maxn

int tot=1,rt1;
int lc[4*maxn],rc[4*maxn],delta[4*maxn],ma[4*maxn];

int figure(int x,int l,int r){
    return ma[x]+delta[x];
}

void push_up(int x,int l,int r){
    int mid=l+r>>1;
    ma[x]=max(figure(lc[x],l,mid),figure(rc[x],mid+1,r));
}

void push_down(int x,int l,int r){
    delta[lc[x]]+=delta[x];
    delta[rc[x]]+=delta[x];
    delta[x]=0;
    push_up(x,l,r);
}

void build(int x,int l,int r){
    if(l==r){
        ma[x]=-INF;
        return;
    }
    int mid=l+r>>1;
    lc[x]=tot++;
    build(lc[x],l,mid);
    rc[x]=tot++;
    build(rc[x],mid+1,r);
    push_up(x,l,r);
}

void change(int x,int l,int r,int goal,int val){
    if(l==r){
        if(val>delta[x]+ma[x]){
            delta[x]=0;
            ma[x]=val;
        }
        return;
    }
    push_down(x,l,r);
    int mid=l+r>>1;
    if(goal<=mid){
        change(lc[x],l,mid,goal,val);
    }else{
        change(rc[x],mid+1,r,goal,val);
    }
    push_up(x,l,r);
}

void add(int x,int l,int r,int ql,int qr,int val){
    if(ql<=l&&r<=qr){
        delta[x]+=val;
        return;
    }
    int mid=l+r>>1;
    if(ql<=mid){
        add(lc[x],l,mid,ql,qr,val);
    }
    if(qr>mid){
        add(rc[x],mid+1,r,ql,qr,val);
    }
    push_up(x,l,r);
}

int query(int x,int l,int r,int ql,int qr){
    if(ql<=l&&r<=qr){
        return figure(x,l,r);
    }
    push_down(x,l,r);
    int mid=l+r>>1,lre=-INF;
    if(ql<=mid){
        lre=max(lre,query(lc[x],l,mid,ql,qr));
    }
    if(qr>mid){
        lre=max(lre,query(rc[x],mid+1,r,ql,qr));
    }
    return lre;
}

void solve(){
//    memset(dp1,-0x3f,sizeof(dp1));
//    memset(dp2,-0x3f,sizeof(dp2));
//    cout<<dp1[1]<<endl;
    tot=1;rt1=0;//rt2=0;
    n=read();//n+1 is the beginning 0
    for(int i=1;i<=n;i++){
        brr[i]=arr[i]=read();
    }
    brr[n+1]=0;
    sort(brr+1,brr+n+2);
    m=unique(brr+1,brr+n+2)-brr-1;
    build(rt1,1,m);

    change(rt1,1,m,1,0);//change(rt2,1,m,1,0);
//    dp1[1]=dp2[1]=0;ind[0]=1;
    for(int i=1;i<=n;i++){
        ind[i]=lower_bound(brr+1,brr+m+1,arr[i])-brr;
        //block not moved:plus arr[i]
        //or:find the one in the same row or cow,for max
        //dp1[j]=dp[ind[i]]][j]
        //dp2[j]=dp[j][ind[i]]
        if(arr[i]==arr[i-1]){
            add(rt1,1,m,1,m,arr[i]);
            int lre=query(rt1,1,m,1,m);
            change(rt1,1,m,ind[i],lre);
        }else{
            /*int lre=max(query(rt2,1,m,1,m),query(rt2,1,m,ind[i],ind[i])+arr[i]);
            change(rt1,1,m,ind[i-1],0ll);change(rt1,1,m,ind[i-1],lre);
            lre=max(query(rt1,1,m,1,m),query(rt1,1,m,ind[i],ind[i])+arr[i]);
            change(rt2,1,m,ind[i-1],0ll);change(rt2,1,m,ind[i-1],lre);*/
            int lre=max(query(rt1,1,m,1,m),query(rt1,1,m,ind[i],ind[i])+arr[i]);
            change(rt1,1,m,ind[i-1],0ll);change(rt1,1,m,ind[i-1],lre);
        }
        /*for(int j=1;j<=m;j++){
            dp1[j]=nxt1[j];
            dp2[j]=nxt2[j];
        }*/
    //    show_arr(dp1);
    //    show_arr(dp2);
    }

    /*int res=0;
    for(int i=1;i<=n;i++){
        res=max(res,max(dp1[i],dp2[i]));
    }
    printf("%lld\n",res);*/
    printf("%lld\n",query(rt1,1,m,1,m));
}

signed main(){

    freopen("color.in","r",stdin);
    freopen("color.out","w",stdout);

    int t=read();
    while(t--){
        solve();
    }

    return 0;
}
/*
1
3
3 5 3

1
8
3 5 2 5 1 2 1 4
*/
